package com.zyq.controller;

import com.zyq.annotation.Log;
import com.zyq.annotation.PreAuthorize;
import com.zyq.entity.SysPermission;
import com.zyq.service.IPermissionService;
import com.zyq.util.R;
import com.zyq.vo.req.PermissionAddReqVo;
import com.zyq.vo.req.PermissionUpdateReqVo;
import com.zyq.vo.resp.PermissionRespNodeTreeVo;
import com.zyq.vo.resp.PermissionRespNodeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * 菜单权限管理相关接口
 */
@RestController
@RequestMapping("/permissions")
public class PermissionController extends BaseController{

    @Autowired
    private IPermissionService permissionService;

    /**
     * 获取所有权限（包括按钮）  角色添加/编辑/分配权限时用到的结果数据
     * @return
     */
    @Log(module = "组织管理-菜单权限管理",action = "获取所有的菜单权限数据接口")
    @PreAuthorize(authorize = "sys:role:add OR sys:role:update")
    @GetMapping("/tree/all")
    public R<List<PermissionRespNodeVo>> getAllPermissionTree(){
        return R.ok(permissionService.selectAllTree());
    }

    /**
     * 菜单权限接口
     * 菜单权限表格展示-获取所有的菜单权限数据接口
     * @return
     */
    @PreAuthorize(authorize = "sys:permission:list")
    @Log(module = "组织管理-菜单权限管理",action = "权限表格列表数据展示")
    @GetMapping("/list")
    public R<List<SysPermission>> getAllPermission(){
        return R.ok(permissionService.selectAll());
    }

    /**
     * 菜单权限树
     * 只递归查询目录与菜单，不要按钮，添加/编辑-权限的上级选择权限树结构展示
     * @return
     */
    @Log(module = "组织管理-菜单权限管理",action = "新增菜单权限接口")
    @PreAuthorize(authorize = "sys:permission:update OR sys:permission:add")
    @GetMapping("/tree")
    public R<List<PermissionRespNodeTreeVo>> getAllPermissionTreeExBtn(){
        return R.ok(permissionService.selectAllMenuByTree());
    }

    /**
     *新增权限
     * @param permissionAddReqVO
     * @return
     */
    @PreAuthorize(authorize = "sys:permission:add")
    @Log(module = "组织管理-菜单权限管理",action = "新增菜单权限接口")
    @PostMapping("/create")
    public R<String> createPermission(@RequestBody @Valid PermissionAddReqVo permissionAddReqVO){
        return permissionService.addPermission(permissionAddReqVO);
    }

    /**
     * 更新权限
     * @param permissionUpdateReqVo
     * @return
     */
    @PreAuthorize(authorize = "sys:permission:update")
    @Log(module = "组织管理-菜单权限管理",action = "编辑菜单权限接口")
    @PostMapping("/update")
    public R<String> updatePermission(@RequestBody @Valid PermissionUpdateReqVo permissionUpdateReqVo){
        return permissionService.updatePermission(permissionUpdateReqVo);
    }

    /**
     * 删除权限
     * @param permissionId
     * @return
     */
    @PreAuthorize(authorize = "sys:permission:delete")
    @Log(module = "组织管理-菜单权限管理",action = "删除菜单权限接口")
    @PostMapping("/{permissionId}")
    public R<String> deletedPermission(@PathVariable("permissionId") String permissionId){
        return permissionService.deletedPermission(permissionId);
    }
}
